import React, { useState, useEffect } from 'react'
import { Space, Button, Tag } from "antd";
import AddLeave from "../components/AddLeave";
import StudentLeave from '../components/StudentLeave';
import leaveModel from '../model/leaveModel';
import moment from 'moment';
import 'moment/locale/zh-cn';

moment.locale('zh-cn');

export default function Student(props: any) {

    const [columns, setColumns] = useState([{
        title: '请假理由',
        dataIndex: 'reason',
        key: 'reason',
    }, {
        title: '开始时间',
        dataIndex: 'start',
        key: 'start',
    }, {
        title: '结束时间',
        dataIndex: 'end',
        key: 'start',
    }, {
        title: '审批状态',
        render: (text: any, record: any) => (
            <Space size="middle">
                {/* if (record.type===1) {
                             
                } */}
                {/* <Tag color="success">已同意</Tag>
                <Tag color="error">已拒绝</Tag> */}
                {record.type ===1?<Tag color="warning">待审批</Tag>:(record.type===2?<Tag color="success">已同意</Tag>:<Tag color="error">已拒绝</Tag> )}
       

            </Space>
        )
    }])

    const [data, setData] = useState<any>([]);



    useEffect(() => {
        leaveModel.student().then(res => {
            if (res.data.code === 1) {
                setData(res.data.list.map((item:any)=>{
                    item.start = moment(item.start).format("YYYY-MM-DD  HH:mm:ss");
                    item.end = moment(item.end).format("YYYY-MM-DD  HH:mm:ss")
                    return  item;
                
                }))
            } else {
                props.history.push("/");
            }
        })
    }, [])

    let  addDateItem = (item:any)=>{
        let  start = moment(item.start).format("YYYY-MM-DD  HH:mm:ss");
        let  end = moment(item.end).format("YYYY-MM-DD  HH:mm:ss");
        setData([...data,{...item,start,end}])

    }



    return (
        <>
            <AddLeave addDateItem={addDateItem}></AddLeave>
            <StudentLeave columns={columns} data={data}></StudentLeave>
        </>
    )
}
